package org.zjx.command;


import org.zjx.command.basic.ClickCommand;
import org.zjx.command.basic.GameMoveCommand;
import org.zjx.command.basic.ScreenshotCommand;
import org.zjx.command.basic.DelayCommand;
import org.zjx.command.identify.IdentifyAddressCommand;
import org.zjx.command.service.NpcServerCommand;
import org.zjx.command.service.ScreenServerCommand;
import org.zjx.core.IWindow;
import org.zjx.entity.Address;
import org.zjx.model.game.Npc;
import org.zjx.model.game.Screen;
import org.zjx.state.StateManager;

import java.awt.image.BufferedImage;

/**
 * 指令工厂 - 统一管理所有指令的创建
 */
public class CommandFactory {

    // 直接执行方法（不加入队列）
    public static boolean executeDirectly(ICommand command) {
        try {
            return command.execute();
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            return false;
        } catch (Exception e) {
            return false;
        }
    }


    public static Screen getScreenById(IWindow targetWindow, int screenId) {
        ScreenServerCommand screenServerCommand = new ScreenServerCommand(targetWindow, screenId);
        screenServerCommand.execute();
        return screenServerCommand.getScreen();
    }

    public static Screen getScreenByName(IWindow targetWindow, String screenName) {
        ScreenServerCommand screenServerCommand = new ScreenServerCommand(targetWindow, screenName);
        screenServerCommand.execute();
        return screenServerCommand.getScreen();
    }

    public static Npc getNpc(IWindow targetWindow, String npcName, int screenId) {
        NpcServerCommand npcServerCommand = new NpcServerCommand(targetWindow, npcName, screenId);
        npcServerCommand.execute();
        return npcServerCommand.getNpc();
    }

    // 快捷截图方法
    public static Address getAddress(IWindow window, BufferedImage screenshot) {
        IdentifyAddressCommand identifyAddressCommand = new IdentifyAddressCommand(window, null, screenshot);
        identifyAddressCommand.execute();
        return identifyAddressCommand.getAddress();
    }

    public static void gameClick(IWindow window, StateManager stateManager, int x, int y) {
        gameClick(window, stateManager,false, x, y);
    }

    public static void gameClick(IWindow window, StateManager stateManager, boolean requiresActiveWindow, int x, int y) {
        GameMoveCommand gameMoveCommand = new GameMoveCommand(window, stateManager, x, y,3);
        gameMoveCommand.execute();
        waitDirectly(window,30);
        ClickCommand clickCommand = new ClickCommand(window,requiresActiveWindow,1,1);
        clickCommand.execute();
        waitDirectly(window,300);
    }

    // 快捷截图方法
    public static BufferedImage screenshot(IWindow window) {
        ScreenshotCommand command = new ScreenshotCommand(window);
        executeDirectly(command);
        return command.getScreenshot();
    }

    // 快捷等待方法
    public static void waitDirectly(IWindow window, long delay) {
        executeDirectly(new DelayCommand(window, delay));
    }
}
